/**
 *
 * @author 
 *
 */
class ControlManager  {
	public constructor() {
    	this.init();
	}
	
    public static mInstance: ControlManager;

    public static getInstance(): ControlManager
    {
        if(ControlManager.mInstance == null)
        {
            ControlManager.mInstance = new ControlManager;
        }

        return ControlManager.mInstance;
    }
    
    private mySelf:ScenePlayer;
    
    public init():void
    {
        this.mySelf = SceneManager.getInstance().mySelf;
    }

    
    private doAction(type:number, tx:number, ty:number):void
    {
    }
    
    
    public doMove(tx:number, ty:number):void
    {
        var pt:egret.Point   = MapTools.getLogicByXY(tx, ty);
        var mypt:egret.Point = MapTools.getLogicByXY(this.mySelf.x, this.mySelf.y);
        var tpt: egret.Point = MapTools.getLocalByPoint(pt);
        
        var dis: number = Math.round(egret.Point.distance(mypt, pt));
        var dir: number = MapTools.angleToArpgDir( MapTools.getAngle(mypt.x, mypt.y, pt.x, pt.y) )
        this.mySelf.setDir(dir);
        egret.log('dis' + ','  + dis + ',' + pt.x + "," + pt.y + "," + dir);
        egret.Tween.removeTweens(SceneManager.getInstance().mySelf);
        //创建 Tween 对象
        egret.Tween.get(SceneManager.getInstance().mySelf, {
            loop: false,//设置循环播放
            onChange: this.onChange,//设置更新函数
            onChangeObj: this//更新函数作用域
        })
            .to({ x: tpt.x, y: tpt.y}, 500 * dis)//设置2000毫秒内 rotation 属性变为360
            .call(this.onComplete, this, []);//设置回调函数及作用域，可用于侦听动画完成
    }
    
    
    
    private onChange(): void
    {
        var tx:number = this.mySelf.x;
        var ty:number = this.mySelf.y;
        var w: number = document.documentElement.clientWidth;
        var h: number = document.documentElement.clientHeight;
        var mapW:number = 224 * 5;
        var mapH:number = 112 * 10;
        var rect:egret.Rectangle = new egret.Rectangle;
        rect.x = tx - (w >> 1) >> 0;
        rect.y = ty - (h >> 1) >> 0;
        rect.width = w ;
        rect.height = h ;
        if(rect.x < 0) rect.x = 0;
        if(rect.y < 0) rect.y = 0;
        if(rect.right > mapW) rect.x = mapW - w; 
        if(rect.bottom > mapH) rect.y = mapH - h;
        
        SceneManager.getInstance().x = -rect.x >> 0;
        SceneManager.getInstance().y = -rect.y  >> 0;
    }

    private onComplete(): void
    {
    }
}
